<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="网站名称" prop="title">
        <el-input
          v-model="form.title"
          style="width: 300px"
          placeholder="请输入网站名称"
        />
      </el-form-item>
      <el-form-item label="大LOGO" prop="largeLogo">
        <Image-upload v-model="form.largeLogo" :limit="1" :isShowTip="false" />
      </el-form-item>
      <el-form-item label="小LOGO" prop="smallLogo">
        <Image-upload v-model="form.smallLogo" :limit="1" :isShowTip="false" />
      </el-form-item>
      <el-form-item label="公众号二维码" prop="publicQrcode">
        <Image-upload v-model="form.publicQrcode" :limit="1" :isShowTip="false" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { webConfig, webConfigUpdate } from "@/api/system/config";
export default {
  name: "Pic",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 遮罩层
      form: {
        title: "",
        largeLogo: "",
        smallLogo: "",
        publicQrcode: "", // 微信公众号二维码
      },
      rules: {},
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    /** 查询网站信息 */
    getInfo() {
      this.loading = true;
      webConfig({ key: "WEB_SETTING" })
        .then((res) => {
          this.form = JSON.parse(res.data.settingValue);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          webConfigUpdate({
            id: "WEB_SETTING",
            settingValue: JSON.stringify(this.form),
          }).then((res) => {
            this.$message.success("保存成功");
            this.$store.dispatch("settings/changeSetting", {
              key: "logo",
              value: this.form.smallLogo ? this.baseUrl + this.form.smallLogo : "",
            });
          });
        }
      });
    },
  },
};
</script>
